package org.qphoto;

import java.util.List;
import java.util.Random;

import org.qphoto.UserMessageArrayAdapter.ViewHolder;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class MessageArrayAdapter extends ArrayAdapter<Message>{

	private List<Message> list = null;
	public  List<Message> getList() {
		return list;
	}

	public void setList(List<Message> list) {
		this.list = list;
	}

	private Activity context   = null;
	private OnItemChildCreateListener onItemChildCreateListener = null;
	
	public OnItemChildCreateListener getOnItemChildCreateListener() {
		return onItemChildCreateListener;
	}

	public void setOnItemChildCreateListener(
			OnItemChildCreateListener onItemChildCreateListener) {
		this.onItemChildCreateListener = onItemChildCreateListener;
	}

	public MessageArrayAdapter(Activity context, List<Message> list) {
		super(context, 0, list);
		
		this.context = context;
		this.list    = list;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view  = null;
		ViewHolder holder = null;  
		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.messageitem, null);

			holder = new ViewHolder();     
			holder.avatorImageView  = (ImageView) view
					.findViewById(R.id.avator);
			holder.usernameTextView  = (TextView) view
					.findViewById(R.id.username);
			holder.contentTextView   = (TextView) view
					.findViewById(R.id.msgcontent);
			holder.picImageView     = (ImageView) view
					.findViewById(R.id.msgimage);
			holder.posttimeTextView = (TextView) view
					.findViewById(R.id.posttime);

			view.setTag(holder);
		} else {
			view   = convertView;
			holder = (ViewHolder) view.getTag();    
		}
		
		// get message
		Message message = list.get(position);
		if (message.getRealname().equals("") == true) {
			holder.usernameTextView.setText(message.getUsername());
		} else {
			holder.usernameTextView.setText(message.getRealname());
		}
		if (message.getContent().equals("") == true) {
			holder.contentTextView.setText("...");
		} else {
			holder.contentTextView.setText(message.getContent());
		}
		holder.posttimeTextView.setText(message.getShowPostTime());
		
		//if ( message.getAvatar().equals("") == false)
		{
			int ids[] = {R.drawable.avatar_2,
					R.drawable.avatar_1,
					R.drawable.avatar_5,
					R.drawable.avatar_7,
					R.drawable.avatar_3,
					R.drawable.avatar_6,
					R.drawable.avatar_4};
			int id = (int) message.getUserid() - 1;
			holder.avatorImageView.setImageResource(ids[id%7]);
		} 

		// set image
		holder.picImageView.setVisibility(View.VISIBLE);
		if (message.getImageuri().equals("") == false) {
			Bitmap picimage = BitmapFactory.decodeFile(message.getImageuri());
			if (picimage != null) {
				int bmpWidth = picimage.getWidth();
				int bmpHeight = picimage.getHeight();
				holder.picImageView.getLayoutParams().width = 120;
				int vWidth = holder.picImageView.getLayoutParams().width;
				if (bmpWidth < vWidth) {
					holder.picImageView.setScaleType(ScaleType.CENTER);
					holder.picImageView.getLayoutParams().height = bmpHeight;
				} else {
					holder.picImageView.setScaleType(ScaleType.FIT_CENTER);
					holder.picImageView.getLayoutParams().height = bmpHeight
							* vWidth / bmpWidth;
				}
				holder.picImageView.setImageBitmap(picimage);
			} else {
				holder.picImageView.setId(R.drawable.default_image);
				holder.picImageView.getLayoutParams().height = 10;
				holder.picImageView.setVisibility(View.INVISIBLE);
			}
		} else if (message.getAudiouri().equals("") == false) {
			holder.picImageView.setImageResource(R.drawable.default_audioimage);
			holder.picImageView.getLayoutParams().height = 53;
			holder.picImageView.getLayoutParams().width = 156;
		} else if (message.getVideouri().equals("") == false) {
			holder.picImageView.setImageResource(R.drawable.default_videoimage);
			holder.picImageView.getLayoutParams().height = 53;
			holder.picImageView.getLayoutParams().width = 156;
		} else {
			holder.picImageView.setImageResource(R.drawable.default_image);
			holder.picImageView.getLayoutParams().height = 10;
			holder.picImageView.setVisibility(View.INVISIBLE);
		}

		// listener
		if (this.onItemChildCreateListener != null) {
			this.onItemChildCreateListener.OnCreateChildView(view, position,
					holder.avatorImageView, R.id.avator);
			this.onItemChildCreateListener.OnCreateChildView(view, position,
					holder.picImageView, R.id.msgimage);
		}

		return view;
	}
	
	public final class ViewHolder {    
		public ImageView  avatorImageView;
		public TextView   usernameTextView;         
		public TextView   contentTextView;   
		public ImageView  picImageView;  
		public TextView   posttimeTextView; 
	}   
}
